﻿Imports System.ServiceProcess
Imports ATS.WCFServices
Imports System.ServiceModel

Public Class WindowsServiceHost
    Inherits ServiceBase
    Private _svcHost As ServiceHost = Nothing

    Public Sub New()
        InitializeComponent()
        Me.ServiceName = "ATS V2.1 Windows Service Host"
        Me.CanStop = True
        Me.CanPauseAndContinue = True
        Me.AutoLog = True

    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Try
            _svcHost = LoadATSService()
        Catch
            EventLog.WriteEntry("ATS 2.1", vbLf & "Failed to start ATS Windows Service Host.", EventLogEntryType.[Error])
            Throw
        End Try

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        If _svcHost IsNot Nothing Then
            _svcHost.Close()
        End If

        EventLog.WriteEntry("ATS 2.1", vbLf & "ATS 2.1 Windows Service Host has been successfully stopped.", EventLogEntryType.Information)
    End Sub



    Private Function LoadATSService() As ServiceHost
        Dim svcHost As ServiceHost = Nothing

        Try
            ' Create Service Host.
            svcHost = New ServiceHost(GetType(AssetService))

            svcHost.Open()
        Catch ex As Exception
            EventLog.WriteEntry("ATS", ("Unable to load ATS 2.1 Service." & vbLf & "Please refer to the README.TXT for setup and installation instructions." & vbLf & "Exception : ") + ex.Message, EventLogEntryType.[Error])

            Throw ex
        End Try

        Return svcHost
    End Function

End Class
